<?php
namespace app\api\controller\xilumarket;

use app\common\controller\Api;
use think\Db;
use think\Exception;
use app\common\model\xilumarket\ProductOffer AS ProductOfferModel;

class ProductOffer extends Api {

    protected $noNeedLogin = ['lists'];
    protected $noNeedRight = ['*'];
    /**
     * 发布评论
     */
    public function add_offer() {
        $params = $this->request->post('');
        $params['user_id'] = $this->auth->id;
        Db::startTrans();
        try {
            $ret = ProductOfferModel::addOffer($params);
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        Db::commit();
        $this->success('出价成功', $ret);

    }

    /**
     * 我的出价
     */
    public function my_message() {
        $list = ProductOfferModel::where('product_message.user_id', $this->auth->id)
            ->with([
                'product'=>function($query){$query->withField('id,name,thumb_image');},
            ])
            ->order('product_offer.id','desc')
            ->paginate();
        $this->success('查询成功',$list);
    }

    /**
     * 出价列表
     * @throws \think\exception\DbException
     */
    public function lists() {
        $product_id = $this->request->param('product_id',null);
        $pagesize = $this->request->param('pagesize',10);

        $list = ProductOfferModel::where('product_offer.product_id',$product_id)
            ->with([
                'user'=>function($query){$query->withField('id,avatar,nickname');},
            ])
            //->where('product_message.status','2')
            ->order('product_offer.id','desc')
            ->paginate($pagesize);
        $this->success('查询成功', $list);

    }

}